﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;
using LiSAClient.Core.Services;
using LiSAClient.Core.Services.Factories;
using LiSAClient.Core.Viewmodel;
using LiSAClient.WpfClient.Resources.Extensions.DynamicColumns;
using LiSAClient.WpfClient.ViewModel.Projects.ArticleFeedback;

namespace LiSAClient.WpfClient.ViewModel.Projects
{
    public class ProjectAdminProgressViewViewModel : ContentViewModel
    {
        #region Observable Objects
        private Guid ProjectID { get; set; }

        private List<string> ColumnHeaders { get; set; }

        public ColumnConfig ColumnConfig { get; set; }

        private List<AdminOverviewArticleModel> overviewLines;
        public List<AdminOverviewArticleModel> OverviewLines
        {
            get { return this.overviewLines; }
            set { this.Set(() => this.OverviewLines, ref this.overviewLines, value); }
        }

        private AdminOverviewArticleModel selectedRow;
        public AdminOverviewArticleModel SelectedRow
        {
            get { return this.selectedRow; }
            set { this.Set(() => this.SelectedRow, ref this.selectedRow, value); }
        }
        #endregion

        #region Initializers
        public ProjectAdminProgressViewViewModel(Guid projectID)
        {
            this.OverviewLines = new List<AdminOverviewArticleModel>();
            this.ProjectID = projectID;
            this.ColumnConfig = new ColumnConfig { Columns = new List<Column>() };
            this.ColumnHeaders = new List<string>();
        }

        public override void OnPresented()
        {
            // Contains relevant users and other relevant informnation
            var theProject = ServiceFactory.Instance.Get<ProjectService>().GetProjectByID(this.ProjectID);
            var allFeedback = ServiceFactory.Instance.Get<ProjectService>().GetArticleFeedback(this.ProjectID);
            var allLines = new List<AdminOverviewArticleModel>();

            // Prepare headers
            this.ColumnHeaders.Add("Article");
            foreach (var projectStage in theProject.Stages)
            {
                foreach (var user in theProject.Users)
                {
                    this.ColumnHeaders.Add(string.Format("{0} - {1}", user.FullName, projectStage.Description));
                }
            }

            // For each ARTICLE we need make a line that shows each users' choice for each Stage
            foreach (var article in theProject.Articles)
            {
                // Create the line for the article
                var newLine = new AdminOverviewArticleModel { ArticleName = article.Name, ColumnValues = new List<ColumnItem>() };

                newLine.ColumnValues.Add(new ColumnItem
                {
                    // TODO: Enable different colors of text depending on choice
                    CellColor = new SolidColorBrush(Colors.WhiteSmoke),
                    Text = article.Name
                });
                // For each Stage, add selection-choices for each user
                foreach (var projectStage in theProject.Stages)
                {
                    foreach (var user in theProject.Users)
                    {
                        var stageFeedbackForUser = new LiSAService.ArticleFeedback { OptionSelected = "" };
                        var stagefeedbackstuff = allFeedback.Where(x => x.ArticleID == article.ID && x.ReviewerUserID == user.ID && x.Stage == projectStage.Stage) ;
                        if (stagefeedbackstuff != null && stagefeedbackstuff.Any())
                        {
                            stagefeedbackstuff.ToList().Sort((x, y) => x.Version.CompareTo(y.Version));
                            stageFeedbackForUser = stagefeedbackstuff.Last();
                        }
                        newLine.ColumnValues.Add(new ColumnItem
                        {
                            // TODO: Enable different colors of text depending on choice
                            CellColor = new SolidColorBrush(Colors.WhiteSmoke),
                            Text = stageFeedbackForUser.OptionSelected
                        });
                    }
                }
                allLines.Add(newLine);
            }

            this.OverviewLines = allLines;

            // Next we generate our columns!
            var newColumns = new List<Column>();
            var headerCounter = 0;
            foreach (var columnHeader in this.ColumnHeaders)
            {
                if (columnHeader == "Article")
                {
                    newColumns.Add(new Column
                    {
                        Header = columnHeader,
                        DataField = string.Format("ArticleName")
                    });
                }
                else
                {
                    newColumns.Add(new Column
                    {
                        Header = this.ColumnHeaders[headerCounter],
                        DataField = string.Format("ColumnValues[{0}].Text", headerCounter)
                    });
                }
                headerCounter++;
            }
            this.ColumnConfig = new ColumnConfig { Columns = newColumns };
        }
        #endregion
    }
}
